/**
 * 
 */
package info.noahcampbell.meter.management;

import info.noahcampbell.meter.management.plugin.GraphBuilderBean;
import info.noahcampbell.meter.management.plugin.MeterReaderBean;
import info.noahcampbell.meter.management.plugin.RrdManager;
import info.noahcampbell.meter.management.plugin.UnableToFindTemplateException;

import java.io.IOException;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.myfaces.portlet.MyFacesGenericPortlet;

/**
 * @author Noah Campbell
 * @version 1.0
 */
public class MeterReaderPortlet extends MyFacesGenericPortlet {

    /**
     * @see org.apache.myfaces.portlet.MyFacesGenericPortlet#doEdit(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
     */
    @Override
    protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException, IOException {
       
        
        Boolean isPortletModeChanged = (Boolean)request.getAttribute("isPortletModeChanged");
        if (isPortletModeChanged.booleanValue()) {
           setPortletRequestFlag(request);
           nonFacesRequest(request, response, "/meterreader/edit.jsp");
           return;
        }

        facesRender(request, response);
    }
    
    

    /**
     * @see org.apache.myfaces.portlet.MyFacesGenericPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
     */
    @Override
    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
        super.processAction(request, response);
        FacesContext fc = this.facesContext(request, response);
        MeterReaderBean meterReader = (MeterReaderBean)fc.getApplication().getVariableResolver().resolveVariable(fc, "meterReader");
        
    }



    /**
     * @see org.apache.myfaces.portlet.MyFacesGenericPortlet#doHelp(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
     */
    @Override
    protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // TODO Auto-generated method stub
        super.doHelp(request, response);
    }

    /**
     * @see org.apache.myfaces.portlet.MyFacesGenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
     */
    @Override
    protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        PortletPreferences prefs = request.getPreferences();
        PortletSession portletSession = request.getPortletSession();
        
        String templateName = prefs.getValue("meterTemplate", "none");
        String origTemplateName = (String) portletSession.getAttribute("template");
        if(!templateName.equalsIgnoreCase(origTemplateName)) {
            portletSession.setAttribute("template", templateName);
            FacesContext fc = this.facesContext(request, response);
            
            MeterReaderBean bean = 
                (MeterReaderBean) fc.getApplication().getVariableResolver().resolveVariable(fc, "meterReader");
            String id = portletSession.getId();
            try {
                bean.setTemplate(templateName, id);
            } catch (UnableToFindTemplateException e) {
                throw new PortletException(e);
            } catch (Exception e) {
                throw new PortletException(e);
            }
            // set the bean into session scope so GraphServlet can pick it up.
            portletSession.setAttribute(id, bean, PortletSession.APPLICATION_SCOPE);
        }
        
        facesRender(request, response);
    }

    /**
     * @see javax.portlet.GenericPortlet#render(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
     */
    @Override
    public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        PortletSession session = request.getPortletSession();
        PortletMode mode = (PortletMode)session.getAttribute("CurrentPortletMode");

        if (mode == null) {
           mode = request.getPortletMode();       
        }

        if (mode != request.getPortletMode()) {
           request.setAttribute("isPortletModeChanged", Boolean.TRUE);
        } else {
           request.setAttribute("isPortletModeChanged", Boolean.FALSE);
        }

        session.setAttribute("CurrentPortletMode", mode);
        super.render(request, response);
    }
}
